package com.emm.yixun.mobile.ui.personal;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.adapter.ArmoryTeamAdapter;
import com.emm.yixun.mobile.adapter.MainProjectAdapter;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.model.ArmorTeamModel;
import com.emm.yixun.mobile.model.ArmorTeamModel.Messag;
import com.emm.yixun.mobile.model.GetGroupHeroList;
import com.emm.yixun.mobile.model.GetProjectList;
import com.emm.yixun.mobile.model.GetProjectList.ProjectList;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import xf.tools.Loading;

/** 团队排行 */
public class TeamFragment extends Fragment {

	private static final String TAG = "TeamFragment";

	FragmentActivity root = null;

	ListView armory;

	ArrayList<ArmorTeamModel> list_team;

	LinearLayout screen_textview;

	LinearLayout sort_textview;

	TextView colors_text2;

	ImageView icon2;

	TextView colors_text;

	ImageView icon1;

	/** 时间 */
	private int SelectPostion = 0;

	/** 类别 */
	private int SelectPostion2 = 0;

	ArrayList<ArmorTeamModel> list = new ArrayList<ArmorTeamModel>();

	ArmoryTeamAdapter adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.team_frg, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		root = getActivity();
		initView();
		getGroupHeroList((SelectPostion2 + 1) + "" + "", (SelectPostion + 1) + "");
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化view
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午4:57:17
	 */
	private void initView() {
		armory = (ListView) root.findViewById(R.id.team_listview);
		screen_textview = (LinearLayout) root.findViewById(R.id.screen_textview);
		colors_text2 = (TextView) root.findViewById(R.id.colors_text2);
		icon2 = (ImageView) root.findViewById(R.id.icon2);
		sort_textview = (LinearLayout) root.findViewById(R.id.sort_textview);
		colors_text = (TextView) root.findViewById(R.id.colors_text);
		icon1 = (ImageView) root.findViewById(R.id.icon1);

		initBtn();
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化监听事件
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午4:57:24
	 */
	private void initBtn() {

		sort_textview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				colors_text.setTextColor(Color.parseColor("#FD593E"));
				colors_text2.setTextColor(Color.parseColor("#3E3C3D"));
				icon1.setImageResource(R.drawable.paixu_down);
				icon2.setImageResource(R.drawable.shaixuan_up);
				SetsortPopu(0);
			}
		});
		screen_textview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				colors_text.setTextColor(Color.parseColor("#3E3C3D"));
				colors_text2.setTextColor(Color.parseColor("#FD593E"));
				icon1.setImageResource(R.drawable.paixu_up);
				icon2.setImageResource(R.drawable.shaixuan_down);
				SetsortPopu(1);
			}
		});
	}

	View viewproject;
	PopupWindow popuproject;

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 选择条件弹出框
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @param type
	 * @author tangxin
	 * @date 2016年12月7日 下午4:58:02
	 */
	public void SetsortPopu(int type) {
		viewproject = LayoutInflater.from(root).inflate(R.layout.mainproject_popu, null);
		popuproject = new PopupWindow(viewproject, android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT,
				android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT);
		popuproject.setFocusable(true);
		popuproject.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
		popuproject.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		popuproject.showAtLocation(sort_textview, Gravity.TOP, 0, 0);
		popuproject.setFocusable(true);
		popuproject.setOutsideTouchable(true);
		viewproject.setFocusable(true);
		viewproject.setFocusableInTouchMode(true);
		viewproject.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					popuproject.dismiss();
					EmmApplication.setLog(root);
				}
				return false;
			}
		});

		viewproject.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				popuproject.dismiss();
				EmmApplication.setLog(root);
			}
		});

		ListView popu_list = (ListView) viewproject.findViewById(R.id.popu_list);
		RelativeLayout top_relativelayout = (RelativeLayout) viewproject.findViewById(R.id.top_relativelayout);
		BaseActivity.SetViewPaddingHeight(top_relativelayout, 95);
		ArrayList<ProjectList> ll = new ArrayList<ProjectList>();
		if (type == 1) {

			ProjectList m = new ProjectList();
			m.setProjectCode("1");
			m.setProjectName("今日");
			ll.add(m);

			ProjectList m2 = new ProjectList();
			m2.setProjectCode("2");
			m2.setProjectName("昨日");
			ll.add(m2);

			ProjectList m3 = new ProjectList();
			m3.setProjectCode("3");
			m3.setProjectName("本周");
			ll.add(m3);

			ProjectList m4 = new ProjectList();
			m4.setProjectCode("4");
			m4.setProjectName("上周");
			ll.add(m4);

			ProjectList m5 = new ProjectList();
			m5.setProjectCode("5");
			m5.setProjectName("本月");
			ll.add(m5);

			ProjectList m6 = new ProjectList();
			m6.setProjectCode("6");
			m6.setProjectName("上月");
			ll.add(m6);

			ProjectList m7 = new ProjectList();
			m7.setProjectCode("7");
			m7.setProjectName("三月内");
			ll.add(m7);

			ProjectList m8 = new ProjectList();
			m8.setProjectCode("8");
			m8.setProjectName("全部");
			ll.add(m8);

			final GetProjectList list = new GetProjectList();
			list.setProjectList(ll);

			MainProjectAdapter adapter = new MainProjectAdapter(root, list);
			popu_list.setAdapter(adapter);
			adapter.SetType(1);
			if (list.getProjectList().size() >= (SelectPostion + 1)) {
				adapter.SeleCt(SelectPostion);
				colors_text2.setText(list.getProjectList().get(SelectPostion).getProjectName());
			} else {
				adapter.SeleCt(0);
				colors_text2.setText(list.getProjectList().get(0).getProjectName());
			}
			popu_list.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					popuproject.dismiss();
					SelectPostion = position;
					EmmApplication.setLog(root);
					colors_text2.setText(list.getProjectList().get(position).getProjectName());
					int numbertype = SelectPostion + 1;
					Log.v(TAG, "numbertype:==>" + numbertype);
					getGroupHeroList((SelectPostion2 + 1) + "" + "", numbertype + "");

				}
			});

		} else {
			ProjectList m = new ProjectList();
			m.setProjectCode("1");
			m.setProjectName("按数量");
			ll.add(m);

			ProjectList m2 = new ProjectList();
			m2.setProjectCode("2");
			m2.setProjectName("按金额");
			ll.add(m2);

			final GetProjectList list = new GetProjectList();
			list.setProjectList(ll);

			MainProjectAdapter adapter = new MainProjectAdapter(root, list);
			popu_list.setAdapter(adapter);
			adapter.SetType(1);

			if (list.getProjectList().size() >= (SelectPostion2 + 1)) {
				adapter.SeleCt(SelectPostion2);
				colors_text.setText(list.getProjectList().get(SelectPostion2).getProjectName());
			} else {
				adapter.SeleCt(0);
				colors_text.setText(list.getProjectList().get(0).getProjectName());
			}
			popu_list.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					popuproject.dismiss();
					SelectPostion2 = position;
					EmmApplication.setLog(root);
					colors_text.setText(list.getProjectList().get(position).getProjectName());
					int numbertype2 = SelectPostion2 + 1;
					Log.v(TAG, "numbertype2:==>" + numbertype2);
					getGroupHeroList(numbertype2 + "", (SelectPostion + 1) + "");
				}
			});

		}
	}

	/**
	 * 团队排名
	 */
	GetGroupHeroList getgrop;

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 团队排名
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @param cusType
	 * @param timeType
	 * @author tangxin
	 * @date 2016年12月7日 下午4:59:45
	 */
	private void getGroupHeroList(String cusType, String timeType) {
		EmmApplication.updateUrl("getGroupHeroList");
		Map<String, String> map = new HashMap<String, String>();
		map.put("version", EmmApplication.getPackageManager(5));
		map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
		map.put("token", EmmApplication.getToken());
		map.put("userId", EmmApplication.getUserId());
		map.put("terminalCode", EmmApplication.getPackageManager(2));
		map.put("projectCode", EmmApplication.getProjectCode());
		map.put("timeType", timeType);
		map.put("cusType", cusType);
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("params", map);
		JSONObject json = new JSONObject(paramsMap);
		String jsonstr = json.toString();
		RequestParams params = new RequestParams();
		params.put("body", jsonstr);
		Log.v("getGroupHeroList-->post", EmmApplication.urlStr + "?body=" + jsonstr);
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束error:" + error + "==" + content);
			}

			@Override
			public void onFinish() {
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束");
				super.onFinish();
			}

			@Override
			public void onStart() {
				super.onStart();
				Loading.showDialogForLoading(root, "正在加载...", false, true, 60000);
				Log.v(TAG, "请求开始");
			}

			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				if (content == null) {
					return;
				}
				Log.v(TAG, content);
				JSONObject jsonObj = JSONObject.parseObject(content);
				JSONObject resData = (JSONObject) jsonObj.get("resData");
				if (resData == null) {
					Log.v(TAG, "信息返回值为空");
					return;
				}

				getgrop = JSONObject.parseObject(resData.toString(), GetGroupHeroList.class);

				if ("1".equals(getgrop.getResult())) {
					Log.v("获取成功", "");

					list.clear();

					ArmorTeamModel a = new ArmorTeamModel();
					a.setTitle("认筹");
					ArrayList<Messag> messag = new ArrayList<ArmorTeamModel.Messag>();
					if (null != getgrop.getGroupHeroList() && getgrop.getGroupHeroList().size() > 0) {
						for (int i = 0; i < getgrop.getGroupHeroList().size(); i++) {
							Messag m = new Messag();
							m.setName(getgrop.getGroupHeroList().get(i).getGroupName());
							m.setNumber(getgrop.getGroupHeroList().get(i).getCusNum());
							m.setPrice(getgrop.getGroupHeroList().get(i).getCusAmount());
							messag.add(m);
						}
						a.setMessag(messag);
						list.add(a);
					} else {
						for (int i = 0; i < 3; i++) {
							Messag m = new Messag();
							m.setName("???");
							m.setNumber("0");
							m.setPrice("0");
							messag.add(m);
						}
						a.setMessag(messag);
						list.add(a);
					}

					ArmorTeamModel a1 = new ArmorTeamModel();
					a1.setTitle("认购");
					ArrayList<Messag> messag2 = new ArrayList<ArmorTeamModel.Messag>();
					if (null != getgrop.getGroupHeroSubList() && getgrop.getGroupHeroSubList().size() > 0) {
						for (int i = 0; i < getgrop.getGroupHeroSubList().size(); i++) {
							Messag m = new Messag();
							m.setName(getgrop.getGroupHeroSubList().get(i).getGroupName_sub());
							m.setNumber(getgrop.getGroupHeroSubList().get(i).getCusNum_sub());
							m.setPrice(getgrop.getGroupHeroSubList().get(i).getCusAmount_sub());
							messag2.add(m);
						}
						a1.setMessag(messag2);
						list.add(a1);
					} else {
						for (int i = 0; i < 3; i++) {
							Messag m = new Messag();
							m.setName("???");
							m.setNumber("0");
							m.setPrice("0");
							messag2.add(m);
						}
						a1.setMessag(messag2);
						list.add(a1);
					}

					ArmorTeamModel a2 = new ArmorTeamModel();
					a2.setTitle("签约");
					ArrayList<Messag> messag3 = new ArrayList<ArmorTeamModel.Messag>();
					if (null != getgrop.getGroupHeroSignedList() && getgrop.getGroupHeroSignedList().size() > 0) {
						for (int i = 0; i < getgrop.getGroupHeroSignedList().size(); i++) {
							Messag m = new Messag();
							m.setName(getgrop.getGroupHeroSignedList().get(i).getGroupName_sig());
							m.setNumber(getgrop.getGroupHeroSignedList().get(i).getCusNum_sig());
							m.setPrice(getgrop.getGroupHeroSignedList().get(i).getCusAmount_sig());
							messag3.add(m);
						}
						a2.setMessag(messag3);
						list.add(a2);
					} else {
						for (int i = 0; i < 3; i++) {
							Messag m = new Messag();
							m.setName("???");
							m.setNumber("0");
							m.setPrice("0");
							messag3.add(m);
						}
						a2.setMessag(messag3);
						list.add(a2);
					}

					adapter = new ArmoryTeamAdapter(root, list);
					armory.setAdapter(adapter);
					adapter.SetType((SelectPostion2 + 1));

				} else {
					EmmApplication.T(getgrop.getErrorMsg().toString());
					Log.v("获取失败", "errorCode:" + getgrop.getErrorCode().toString() + "errorMsg:" + getgrop.getErrorMsg().toString());
				}
			}
		});
	}
}
